package com.bc.ui.setting;

import java.util.Random;

import net.youmi.android.diy.DiyManager;

import org.jraf.android.backport.switchwidget.Switch;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import cn.waps.AppConnect;

import com.bc.R;
import com.bc.ui.BaseActivity;
import com.bc.ui.BtcCacheOperator;
import com.bc.utils.CommonUtils;
import com.bc.utils.Constants;
import com.bc.utils.SettingPrefs;
import com.umeng.fb.FeedbackAgent;

public class SettingActivity extends BaseActivity implements OnClickListener{
	
	private TextView tvRefreshTime;
	
	private String[] refreshArray;
	private int[] values;
	private Switch exitSwitch;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_setting);
		
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowHomeEnabled(true);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setTitle(R.string.setting);
		
		findViewById(R.id.bitcoin_setting).setOnClickListener(this);
		findViewById(R.id.refresh_setting).setOnClickListener(this);
		findViewById(R.id.feedback).setOnClickListener(this);
		findViewById(R.id.app_recommend).setOnClickListener(this);
		
		refreshArray = getResources().getStringArray(R.array.refresh_time_array);
		values = getResources().getIntArray(R.array.refresh_time_value_array);
		
		tvRefreshTime = (TextView)findViewById(R.id.refresh_time_text);
		tvRefreshTime.setText(getString(R.string.refresh_time_text, 
				refreshArray[SettingPrefs.getInstance(SettingActivity.this)
								.getIntFromPrefs(SettingPrefs.REFRESH_TIME_POSITION, 0)]));
		exitSwitch = (Switch)findViewById(R.id.setting_switch);
		exitSwitch.setChecked(SettingPrefs.getInstance(SettingActivity.this).getBooleanFromPrefs(SettingPrefs.EXIT_SERVICE_ON, false));
		exitSwitch.setOnCheckedChangeListener(checkedChangeListener);
	}
	
	OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			SettingPrefs.getInstance(SettingActivity.this).putBooleanToPrefs(SettingPrefs.EXIT_SERVICE_ON, isChecked);
			if (isChecked) {
				CommonUtils.sendNotification(SettingActivity.this, getString(R.string.app_name), getString(R.string.service_on_tip));
				Toast.makeText(SettingActivity.this, getString(R.string.exit_service_on), Toast.LENGTH_SHORT).show();
			} else {
				CommonUtils.cancelNotification(SettingActivity.this, BtcCacheOperator.BACKGROUND_NOTIFI_ID);
			}
		}
	};
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
			overridePendingTransition(R.anim.push_in_left, R.anim.push_out_right);
		}
		return true;
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bitcoin_setting:
			Intent intent = new Intent(SettingActivity.this, BitcoinSettingActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.push_in_right, R.anim.push_out_left);
			break;
		case R.id.refresh_setting:
			showRefreshTimeDialog();
			break;
		case R.id.feedback:
			FeedbackAgent agent = new FeedbackAgent(SettingActivity.this);
			agent.startFeedbackActivity();
			break;
		case R.id.app_recommend:
			
			Random random = new Random();
			int result = random.nextInt(3);
			if (result == 0) {
				AppConnect.getInstance(this).showAppOffers(this);
			} else if (result == 1){
				DiyManager.showRecommendWall(this); 
			} else {
				DiyManager.showRecommendGameWall(this);
			}
//			if (Constants.RELEASE_WAPS) {
//				AppConnect.getInstance(this).showAppOffers(this);
//			} else {
//				DiyManager.showRecommendWall(this); 
//			}
			overridePendingTransition(R.anim.push_in_right, R.anim.push_out_left);
			break;
		default:
			break;
		}
	}
	
	private Dialog dialog;
	public void showRefreshTimeDialog() {
		if (dialog != null && dialog.isShowing()) {
			 return;
		}
		dialog = new AlertDialog.Builder(this)
		.setTitle(R.string.refresh_setting)
		.setItems(refreshArray, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (values.length < which) {
					dialog.dismiss();
					return;
				}
				tvRefreshTime.setText(getString(R.string.refresh_time_text, refreshArray[which]));
				dialog.dismiss();
				SettingPrefs.getInstance(SettingActivity.this).putIntToPrefs(SettingPrefs.REFRESH_TIME_POSITION, which);
				SettingPrefs.getInstance(SettingActivity.this).putIntToPrefs(SettingPrefs.REFRESH_TIME, values[which]);
				
			}
		}).show();
	}
}
